package Classic150;

public class 反转字符串中的单词 {
    public String reverseWords(String s) {
        /*
            容易想到的解法，不借助api的方法参考hot100同题
         */
        StringBuilder sb = new StringBuilder();
        int end = s.length() - 1;
        while (true) {
            while (end >= 0 && s.charAt(end) == ' ') end--;
            if (end < 0) break;
            int start = end;
            while (start >= 0 && s.charAt(start) != ' ') start--;
            sb.append(s, start + 1, end + 1);
            sb.append(' ');
            end = start;
        }
        sb.deleteCharAt(sb.length() - 1);
        return sb.toString();
    }
}
